// 菜品文件，存储菜品相关操作
#include <stdio.h>
#include "dish.h"
#include "struct.h"
#include <string.h>
// 打印所有菜品
void select_all_dish(Dish* dish, int count) {
    if(count == 0) {
        printf("没有菜品哦！\n");
        return;
    }
    printf("-----------------------------------\n");
    printf("|  序号    菜名      价格     库存  |\n");
    for(int i = 0; i < count; i++) {
        printf("|  %-4d   %-12s   %-6.2f   %-4d  |\n", 
               i + 1, dish[i].name, dish[i].price, dish[i].stock);
    }
    printf("-----------------------------------\n");
}

// 添加菜品
int add_dish(Dish* dish, int count) {
    printf("请输入添加菜品名称：");
    scanf("%s", dish->name);
    printf("请输入价格：");
    scanf("%f", &dish->price);
    printf("请输入库存：");
    scanf("%d", &dish->stock);
    dish->id = count + 1;
    return 1; 
}

// 删除菜品
void delete_dish(Dish* dish, int id, int count) {
    for(int i = id - 1; i < count - 1; i++) {
        dish[i] = dish[i + 1];
        dish[i].id = i + 1;
    }
}

// 查找菜品（修改为使用strcmp比较UTF-8字符串）
int find_dish(Dish* dish, char* name, int count, Dish **pd) {
    for(int i = 0; i < count; i++) {
        if(strcmp(dish[i].name, name) == 0) {
            *pd = &dish[i];
            return i+1;
        }
    }
    return 0;
}

// 更新菜品信息
void update(Dish* dishs, int count, int id) {
    int n = 0;
    Dish* dish = NULL;
    for(int i = 0; i < count; i++) {
        if(dishs[i].id == id) {
            dish = &dishs[i];
            break;
        }
    }
    if(dish == NULL) {
        printf("未找到该菜品！\n");
        return;
    }
    
    printf("\n1.修改菜品名\n");
    printf("2.修改价格\n");
    printf("3.修改库存\n");
    printf("请输入选择：");
    scanf("%d", &n);
    
    switch(n) {
        case 1:
            printf("新的名称：");
            scanf("%s", dish->name);
            break;
        case 2:
            printf("新价格：");
            scanf("%f", &dish->price);
            break;
        case 3:
            printf("新的库存：");
            scanf("%d", &dish->stock);
            break;
        default: 
            printf("无效选择！\n");
            return;
    }
}

//------------------------------------------------------
// 窗口操作函数

/**
 * @brief 添加菜品
 * 
 * 向餐馆菜品库中添加新菜品，分配唯一ID
 * 
 * @param pw 指向当前窗口的指针
 * @return int 执行状态：0=失败，1=成功
 */
int Dish_add(Window *pw) {
    if(pw->dish_count >= MAX_DISH_PER_WINDOW) {
        printf("菜单已满！\n");
        return 0;
    }
    add_dish(&pw->dishes[pw->dish_count], pw->dish_count);
    pw->dish_count++;
    select_all_dish(pw->dishes, pw->dish_count);
    return 1;
}

/**
 * @brief 删除菜品
 * 
 * 
 * @param pw 指向当前窗口的指针
 * @return int 执行状态：0=失败，1=成功
 */
int Dish_delete(Window *pw) {
    int id;
    select_all_dish(pw->dishes, pw->dish_count);
    if(pw->dish_count == 0) return 0;
    
    printf("请选择要删除的菜品编号：");
    scanf("%d", &id);
    if(id < 1 || id > pw->dish_count) {
        printf("无效的菜品编号！\n");
        return 0;
    }
    delete_dish(pw->dishes, id, pw->dish_count);
    pw->dish_count--;
    printf("删除成功！\n");
    return 1;
}

/**
 * @brief 更新菜品
 * 
 * 
 * @param pw 指向当前窗口的指针
 * @return int 执行状态：0=失败，1=成功
 */
int Dish_update(Window *pw) {
    int id; 
    select_all_dish(pw->dishes, pw->dish_count);
    if(pw->dish_count == 0) return 0;
    
    printf("请选择要修改的菜品编号：");
    scanf("%d", &id);
    if(id < 1 || id > pw->dish_count) {
        printf("无效的菜品编号！\n");
        return 0;
    }
    
    update(pw->dishes, pw->dish_count, id);
    select_all_dish(pw->dishes, pw->dish_count);
    printf("修改成功!\n");
    return 1;
}

/**
 * @brief 查找菜品
 * 
 * 
 * @param pw 指向当前窗口的指针
 * @return int 执行状态：0=失败，1=成功
 */
int Dish_find(Window *pw) {
    char name[DISH_NAME_LEN];
    printf("请输入要查找的菜品名称：");
    scanf("%s", name);
    
    Dish *found_dish = NULL;
    int ret = find_dish(pw->dishes, name, pw->dish_count, &found_dish);
    if(ret > 0) {
        printf("\n查找结果：\n");
        printf("-----------------------------------\n");
        printf("|  序号    菜名      价格     库存  |\n");
        printf("|  %-4d   %-12s   %-6.2f   %-4d  |\n", 
               ret, found_dish->name, found_dish->price, found_dish->stock);
        printf("-----------------------------------\n");
        return 1;
    } else {
        printf("没有找到该菜品！\n");
        return 0;
    }
}
